<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>取得前一次 MySQL 操作所影响的记录行数</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="ref.mysql.html">« MySQL 函数</a></li>
      <li style="float: right;"><a href="function.mysql-client-encoding.html">mysql_client_encoding »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.mysql.html">MySQL 函数</a></li>
    <li>取得前一次 MySQL 操作所影响的记录行数</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.mysql-affected-rows" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">mysql_affected_rows</h1>
  <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">mysql_affected_rows</span> &mdash; <span class="dc-title">取得前一次 MySQL 操作所影响的记录行数</span></p>

 </div>
 
 
 <div id="function.mysql-affected-rows-refsynopsisdiv">
  <div class="warning"><strong class="warning">警告</strong>
   <p class="para">本扩展自 PHP 5.5.0
起已废弃，并在自 PHP 7.0.0 开始被移除。应使用 <a href="book.mysqli.html" class="link">MySQLi</a>
或 <a href="ref.pdo-mysql.html" class="link">PDO_MySQL</a> 扩展来替换之。参见 <a href="mysqlinfo.api.choosing.html" class="link">MySQL：选择 API</a> 
指南来获取更多信息。用以替代本函数的有：</p>
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="mysqli.affected-rows.html" class="function">mysqli_affected_rows()</a></span></li>
    <li class="member"><span class="methodname"><a href="pdostatement.rowcount.html" class="methodname">PDOStatement::rowCount()</a></span></li>
   </ul>
  </div>
 </div>
 
 <div class="refsect1 description" id="refsect1-function.mysql-affected-rows-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>mysql_affected_rows</strong></span>(<span class="methodparam"><span class="type">resource</span> <code class="parameter">$link_identifier</code><span class="initializer"> = NULL</span></span>): <span class="type">int</span></div>

  <p class="para rdfs-comment">
   取得最近一次与 <code class="parameter">link_identifier</code> 关联的
   INSERT，UPDATE 或 DELETE 查询所影响的记录行数。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.mysql-affected-rows-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    <dt>

<code class="parameter">link_identifier</code></dt>
<dd>
<p class="para">MySQL
连接。如不指定连接标识，则使用由 <span class="function"><a href="function.mysql-connect.html" class="function">mysql_connect()</a></span>
最近打开的连接。如果没有找到该连接，会尝试不带参数调用
<span class="function"><a href="function.mysql-connect.html" class="function">mysql_connect()</a></span>
来创建。如没有找到连接或无法建立连接，则会生成
<strong><code>E_WARNING</code></strong> 级别的错误。</p></dd>


   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.mysql-affected-rows-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   执行成功则返回受影响的行的数目，如果最近一次查询失败的话，函数返回 -1。
  </p>
  <p class="para">
   如果最近一次操作是没有任何条件（WHERE）的 DELETE
   查询，在表中所有的记录都会被删除，但本函数返回值在 4.1.2 版之前都为 0。
  </p>
  <p class="para">
   当使用 UPDATE 查询，MySQL 不会将原值和新值一样的列更新。这样使得
   <span class="function"><strong>mysql_affected_rows()</strong></span>
   函数返回值不一定就是查询条件所符合的记录数，只有真正被修改的记录数才会被返回。
  </p>
  <p class="para">
   REPLACE 语句首先删除具有相同主键的记录，然后插入一个新记录。本函数返回的是被删除的记录数加上被插入的记录数。
  </p>
  <p class="para">
   &quot;INSERT ... ON DUPLICATE KEY UPDATE&quot; 这种情况的查询，当执行了一次 INSERT 返回的值会是 <code class="literal">1</code>；如果是对已经存在的记录执行一次 UPDATE 将返回 <code class="literal">2</code>。 
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.mysql-affected-rows-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-1738">
    <p><strong>示例 #1 <span class="function"><strong>mysql_affected_rows()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$link&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mysql_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">'localhost'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'mysql_user'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'mysql_password'</span><span style="color: #007700">);<br />if&nbsp;(!</span><span style="color: #0000BB">$link</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;die(</span><span style="color: #DD0000">'Could&nbsp;not&nbsp;connect:&nbsp;'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">mysql_error</span><span style="color: #007700">());<br />}<br /></span><span style="color: #0000BB">mysql_select_db</span><span style="color: #007700">(</span><span style="color: #DD0000">'mydb'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;本例返回被删除记录的准确数目&nbsp;*/<br /></span><span style="color: #0000BB">mysql_query</span><span style="color: #007700">(</span><span style="color: #DD0000">'DELETE&nbsp;FROM&nbsp;mytable&nbsp;WHERE&nbsp;id&nbsp;&lt;&nbsp;10'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"Records&nbsp;deleted:&nbsp;%d\n"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">mysql_affected_rows</span><span style="color: #007700">());<br /><br /></span><span style="color: #FF8000">/*&nbsp;对于非真值的&nbsp;WHERE&nbsp;子句，应返回&nbsp;0&nbsp;*/<br /></span><span style="color: #0000BB">mysql_query</span><span style="color: #007700">(</span><span style="color: #DD0000">'DELETE&nbsp;FROM&nbsp;mytable&nbsp;WHERE&nbsp;0'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"Records&nbsp;deleted:&nbsp;%d\n"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">mysql_affected_rows</span><span style="color: #007700">());<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Records deleted: 10
Records deleted: 0
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-1739">
    <p><strong>示例 #2 使用事务处理的 <span class="function"><strong>mysql_affected_rows()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$link&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mysql_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">'localhost'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'mysql_user'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'mysql_password'</span><span style="color: #007700">);<br />if&nbsp;(!</span><span style="color: #0000BB">$link</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;die(</span><span style="color: #DD0000">'Could&nbsp;not&nbsp;connect:&nbsp;'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">mysql_error</span><span style="color: #007700">());<br />}<br /></span><span style="color: #0000BB">mysql_select_db</span><span style="color: #007700">(</span><span style="color: #DD0000">'mydb'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;Update&nbsp;records&nbsp;*/<br /></span><span style="color: #0000BB">mysql_query</span><span style="color: #007700">(</span><span style="color: #DD0000">"UPDATE&nbsp;mytable&nbsp;SET&nbsp;used=1&nbsp;WHERE&nbsp;id&nbsp;&lt;&nbsp;10"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">printf&nbsp;</span><span style="color: #007700">(</span><span style="color: #DD0000">"Updated&nbsp;records:&nbsp;%d\n"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">mysql_affected_rows</span><span style="color: #007700">());<br /></span><span style="color: #0000BB">mysql_query</span><span style="color: #007700">(</span><span style="color: #DD0000">"COMMIT"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Updated Records: 10
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.mysql-affected-rows-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <strong>事务处理</strong><br />
   <p class="para">
    如果使用事务处理（transactions），需要在 INSERT，UPDATE 或 DELETE 查询后调用
    <span class="function"><strong>mysql_affected_rows()</strong></span> 函数，而不是在 COMMIT 命令之后。
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <strong>SELECT 语句</strong><br />
   <p class="para">
    要获取 SELECT 所返回的行数，可以用
    <span class="function"><a href="function.mysql-num-rows.html" class="function">mysql_num_rows()</a></span>。
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <strong>Cascaded 外键</strong><br />
   <p class="para">
     <span class="function"><strong>mysql_affected_rows()</strong></span> does not count rows affected
     implicitly through the use of ON DELETE CASCADE and/or ON UPDATE CASCADE 
     in foreign key constraints.
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.mysql-affected-rows-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.mysql-num-rows.html" class="function" rel="rdfs-seeAlso">mysql_num_rows()</a> - 取得结果集中行的数目</span></li>
    <li class="member"><span class="function"><a href="function.mysql-info.html" class="function" rel="rdfs-seeAlso">mysql_info()</a> - 取得最近一条查询的信息</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>